package com.shengyuan.manage_os.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;

import com.android.volley.Request;
import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.base.BaseFragment;
import com.shengyuan.manage_os.im.ECApplication;
import com.shengyuan.manage_os.intface.NetResponses;
import com.shengyuan.manage_os.sqlite.SQLiteTools;
import com.shengyuan.manage_os.ui.ContactListActivity;
import com.shengyuan.manage_os.utils.NetRequest;
import com.shengyuan.manage_os.view.MyListView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import oa.database.bean.Department;

/**
 * Created by Administrator on 2016/11/22.
 */

public class ContactFragment extends BaseFragment implements AdapterView.OnItemClickListener,View.OnClickListener,NetResponses {

    private MyListView msg_list;
    private SimpleAdapter sim_adapter;
    private List<Map<String,Object>> data_list = new ArrayList<>();
    private ContactFragment contactFragment;
    private RelativeLayout group,usual;
    private List<Department> departments;
    private NetRequest netRequst;

    private int[] icon = {
            R.mipmap.icon_concern,
            R.mipmap.default_org_icon,
            R.mipmap.icon_group_conversation};
    private String[] iconName = { "我的关注", "手机通讯录", "我的群组"};

    public ContactFragment(){
        super();
        init();
    }

    public  ContactFragment init(){
//        if(contactFragment == null){
            transaction = manager.beginTransaction();
            contactFragment = this;
            transaction.add(R.id.fragment_content,contactFragment);
            transaction.commit();
//        }
        return contactFragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact,null);
        msg_list = (MyListView) view.findViewById(R.id.list_contact);
        group = (RelativeLayout) view.findViewById(R.id.group);
        usual = (RelativeLayout) view.findViewById(R.id.usual);
        usual.setOnClickListener(this);
        group.setOnClickListener(this);
        data_list = new ArrayList<>();
        getData();
        String [] from ={"image","text"};
        int [] to = {R.id.image,R.id.text};
        sim_adapter = new SimpleAdapter(this.getActivity(), data_list, R.layout.myself_item, from, to);
        msg_list.setAdapter(sim_adapter);
        msg_list.setOnItemClickListener(this);
        departments = SQLiteTools.queryDepartmentS();
        if(departments.size() == 0){
            netRequst = new NetRequest(ECApplication.getInstance());
            netRequst.setNetResponses(this);
            netRequst.request(Request.Method.GET, "http://imserver.myappcc.com/api/Getuseracc", null, "", "CONTACT", 100);
        }


        return view;
    }

    public List<Map<String, Object>> getData(){
        //cion和iconName的长度是相同的，这里任选其一都可以
        for(int i=0;i<icon.length;i++){
            Map<String, Object> map = new HashMap<>();
            map.put("image", icon[i]);
            map.put("text", iconName[i]);
            data_list.add(map);
        }

        return data_list;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position){
            case 0:
//                Intent intent = new Intent(getActivity(), ContactListActivity.class);
//                intent.putExtra("title","我的关注");
//                startActivity(intent);
                break;
        }
    }

    @Override
    public void onClick(View v) {

        Intent intent = new Intent(getActivity(), ContactListActivity.class);
        switch (v.getId()){
            case R.id.group:
                intent.putExtra("title","联系人");
                startActivity(intent);
                break;
            case R.id.usual:
                intent.putExtra("title","常用联系人");
                intent.putExtra("isUsual",true);
                startActivity(intent);
        }
    }

    @Override
    public void success(int type, JSONObject jsonObject) {
        if (type == 100) {
            System.out.println("@@@@@@@@@@@*************"+jsonObject.toString());
        }

    }
}
